package com.easy.project.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.easy.project.domain.dto.system.RoleMenuDTO;
import com.easy.project.domain.po.system.SysRoleMenuPO;
import com.easy.project.domain.po.system.SysUserRolePO;
import com.easy.project.mapper.system.SysRoleMenuMapper;
import com.easy.project.service.system.ISysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统角色菜单关联表 服务实现类
 * </p>
 *
 * @author wxz
 * @since 2024-01-11
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuPO> implements ISysRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void roleMenu(RoleMenuDTO roleMenuDTO) {
        // 删除角色的关联关系
        remove(new LambdaQueryWrapper<SysRoleMenuPO>().eq(SysRoleMenuPO::getRoleId,roleMenuDTO.getRoleId()));
        // 创建关联关系
        List<SysRoleMenuPO> sysRoleMenuPOList = new ArrayList<>();
        List<Long> menuIdList = roleMenuDTO.getMenuIdList();
        Long roleId = roleMenuDTO.getRoleId();
        for (Long menuId : menuIdList){
            SysRoleMenuPO sysRoleMenuPO = new SysRoleMenuPO();
            sysRoleMenuPO.setRoleId(roleId);
            sysRoleMenuPO.setMenuId(menuId);
            sysRoleMenuPOList.add(sysRoleMenuPO);
        }
        saveBatch(sysRoleMenuPOList);
    }
}
